﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Lime49.UI;
using LockCrypt.DAL;
using LockCrypt.Core;

namespace LockCrypt {
    /// <summary>
    /// Window for changing the database password.
    /// </summary>
    public partial class WinChangePassword : Window {
        /// <summary>
        /// Gets or sets the database provider.
        /// </summary>
        /// <value>The provider.</value>
        public IDbProvider Provider {get;set;}

        /// <summary>
        /// Initializes a new <see cref="WinChangePassword"/>.
        /// </summary>
        /// <param name="provider">The provider whose password to change.</param>
        public WinChangePassword(IDbProvider provider) {
            InitializeComponent();
            this.Provider = provider;
        }

        /// <summary>
        /// Checks the data entered is valid.
        /// </summary>
        /// <returns><c>True</c> if the data is valid, otherwise <c>false</c></returns>
        private bool Validate() {
            bool valid = true;
            if(Provider.Encryption.GetKey(txtCurrentPassword.Password) != Provider.Encryption.EncryptionKey) {
                DialogBox.ShowAlert(this, I18nUtils.GetString("Errors", "IncorrectPassword"), I18nUtils.GetString("Errors", "Error"));
                txtConfirmNewPassword.Focus();
                valid = false;
            } else if(txtNewPassword.Password != txtConfirmNewPassword.Password) {
                DialogBox.ShowAlert(this, I18nUtils.GetString("Errors", "PasswordsDontMatch"), I18nUtils.GetString("Errors", "Error"));
                txtNewPassword.Focus();
                valid = false;
            } else if(txtNewPassword.Password.Trim().Length == 0 && DialogBox.Show(this, I18nUtils.GetString("Errors", "PassBlankDesc"), I18nUtils.GetString("Errors", "PasswordBlank"), DialogBox.DialogBoxType.YesNo, DialogBox.DialogBoxIcon.Question, DialogBoxButton.Yes) != DialogBoxButton.Yes) {
                txtNewPassword.Focus();
                valid = false;
            }
            return valid;
        }

        /// <summary>
        /// Changes the password.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs"/> instance containing the event data.</param>
        private void Save(object sender, ExecutedRoutedEventArgs e) {
            if(Validate()) {
                Provider.ChangePassword(txtNewPassword.Password);
                DialogResult = true;
            }
        }

        /// <summary>
        /// Closes the window.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs"/> instance containing the event data.</param>
        private void CloseWindow(object sender, ExecutedRoutedEventArgs e) {
            DialogResult = false;
        }
    }
}
